package com.jxwifi.cloud.quickcleanserver.presenter;

import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jxwifi.cloud.quickcleanserver.app.AccountMgr;
import com.jxwifi.cloud.quickcleanserver.app.UrlPool;
import com.jxwifi.cloud.quickcleanserver.bean.WithdraApplyBean;
import com.jxwifi.cloud.quickcleanserver.model.AuzImpl;
import com.jxwifi.cloud.quickcleanserver.okhttp.AuzHttp;
import com.jxwifi.cloud.quickcleanserver.okhttp.DataJson_Cb;
import com.jxwifi.cloud.quickcleanserver.okhttp.Params;
import com.jxwifi.cloud.quickcleanserver.utils.PasswordLogin;
import com.jxwifi.cloud.quickcleanserver.utils.ToastUtils;
import com.jxwifi.cloud.quickcleanserver.view.MyBalanceView;

import java.util.List;

public class MyBalancePresenter {

    private String TAG = this.getClass().getSimpleName();
    private AuzImpl mAuz;
    private MyBalanceView mView;
    public List<WithdraApplyBean> mListBean;

    public MyBalancePresenter(MyBalanceView mView) {
        this.mView = mView;
        mAuz = new AuzImpl();
    }

    public void doList() {
        Params params = new Params();
        params.add("page", mView.getPageIndex());
        params.add("limit", mView.getPAGE_SIZE());
        Log.i(TAG,"doList() ==> ");
        mAuz.auzget(UrlPool.GET_PAGE_INOUTRECORD, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                mView.getListonSuccess(code, data);
                withdrawal();
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.getListonFailure(errorCode, msg);
            }
        });
    }

    /**
     * 提现申请记录
     */
    public void withdrawal(){
        Params params=new Params();
        params.add("limit",20);
        params.add("page",1);
        Log.i(TAG,"withdrawal ===> ");
        AuzHttp.get(UrlPool.WITHDRAW_APPLY, params, new DataJson_Cb() {
            @Override
            public void onSuccess(int code, String data) {
                //实体类：WithdraApplyBean
                Log.i(TAG,"withdrawal ===> onSuccess code = "+code);
                if (code==0){
                    JSONObject obj = JSON.parseObject(data);
                    Log.i(TAG,"withdrawal ===> data = "+data);
                    String str = obj.getString("records");
                    Log.i(TAG,"withdrawal ===> str = "+str);
                    mListBean = JSON.parseArray(str, WithdraApplyBean.class);
                    //把相同账户的数据过滤
                    Log.i(TAG,"withdrawal ===> mListBean.size() = "+mListBean.size());
                    for (int i = 0; i < mListBean.size()-1; i++) {
                        for (int j = mListBean.size() - 1; j > i; j--) {
                            Log.i(TAG,"withdrawal ==> mListBean.get(j).getBankNum() = "+mListBean.get(j).getBankNum()+" mListBean.get(i).getBankNum() = "+mListBean.get(i).getBankNum());
                            if (mListBean.get(j).getBankNum().equals(mListBean.get(i).getBankNum())) {
                                mListBean.remove(j);
                                Log.i(TAG,"withdrawal ==> 移除");
                            }
                        }
                    }
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {

            }
        },TAG);
    }

    public void doinitleaves() {
        Log.i(TAG,"doinitleaves() ==> ");
        mAuz.auzget(UrlPool.SYSTEM_LIST, new Params(), new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                mView.initleavesonSuccess(code, data);
                mView.dissDialog();
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.initleavesonFailure(errorCode, msg);
                mView.dissDialog();
            }
        });
    }

    public void domemberinfo() {
        Log.i(TAG,"domemberinfo() ==> ");
        mAuz.auzget(UrlPool.GET_UCCLEANER_INFO, new Params(), new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                mView.memberinfoonSuccess(code, data);
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.memberinfoonFailure(errorCode, msg);
            }
        });
    }
}
